<%inherit file="base.html"/>

<%def name="head()">
<script src="/static/plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
<script src="/static/plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>
<link href="/static/plugins/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
<script src="/static/js/jquery.upload.min.js" type="text/javascript"></script>
<link href="/static/js/jquery.upload.css" rel="stylesheet" type="text/css" />
<script>
    $(function () {
        $('#file_upload').uploadifive({
            'buttonText': '上传数据文件',
            'buttonClass': 'btn btn-sm bg-navy',
            'width': 120,
            'lightheight': 20,
            'height':34,
            'queueID': 'queue',            
            'formData': {'_xsrf': '${handler.xsrf_token}'},
            'fileSizeLimit': '100MB',
            'uploadScript': '/admin/backup/upload',
            'onUploadComplete': function (file, data, response) {
                window.location.reload();
            }
        });      
        $('#maintable').dataTable({
          "bPaginate": true,
          "bLengthChange": false,
          "bFilter": false,
          "bSort": true,
          "bInfo": false,
          "bAutoWidth": false,
          "pagingType":   "full_numbers",
          "oLanguage": {
                "sZeroRecords": "没有找到符合条件的数据",
                "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
                }
            }
        });             
    });
</script>
<script>
function dumpdb(){
    $.post("/admin/backup/dump", {_xsrf: '${handler.xsrf_token}'}, function (ev) {
        console.log(ev);
        console.log(ev.msg);
        window.location.reload();
        alert(ev.msg);
    }, "json");
}

function restoredb(bakfs){
    if (!confirm("确认恢复操作吗？")) {
        return;
    }
    $.post("/admin/backup/restore", {bakfs:bakfs, _xsrf: '${handler.xsrf_token}'}, function (ev) {
        console.log(ev);
        console.log(ev.msg);
        window.location.reload();
        alert(ev.msg);
    }, "json");
}

function deletefs(bakfs){
    if (!confirm("确认删除操作吗，此操作不可恢复？")) {
        return;
    }
    $.post("/admin/backup/delete", {bakfs:bakfs, _xsrf: '${handler.xsrf_token}'}, function (ev) {
        console.log(ev);
        console.log(ev.msg);
        window.location.reload();
        alert(ev.msg);
    }, "json");
}
</script>
</%def>

<%def name="body()">
<section class="content">
    <div class="box box-default">
        <div class="box-header">
            <i class="fa fa-database"></i>
            <h3 class="box-title">数据库备份管理</h3>
        </div>
        <div class="box-body">
            <div class="container">
        <form id="query_form" class="form-horizontal form-well" role="form" action="/ops/opslog" method="post">
            <div class="form-group">
                <label for="backup_path" class="col-md-4 control-label">备份目录</label>
                <div class="col-md-4">
                    <input type="text" id="backup_path" name="backup_path" class="form-control" value="${backup_path}" readonly="readonly">
                </div>           
                <div class="col-md-4">
                    % if permit.match(current_user.username,'/admin/backup/dump'):
                    <button type="button" onclick="dumpdb();" class="btn btn-md bg-navy">立即备份</button>
                    % endif
                    % if permit.match(current_user.username,'/admin/backup/upload'):
                    <button id="file_upload" type="button" class="btn btn-lg bg-navy">上传备份</button>
                    % endif                       
                </div>

            </div>   
        </form>
        <div class="clearfix"></div>
        <div id="queue"></div>    


        <table class="table table-hover" id="maintable">
            <thead>
            <tr>
                <th width="60%">备份名称</th>
                <th width="40%">操作</th>
            </tr>
            </thead>
            <tbody>
            % for backup in backups:
            <tr>
                <td>${backup}</td>
                <td>
                    % if permit.match(current_user.username,'/admin/backup/delete'):
                    <a href="javascript:deletefs('${backup}')">删除</a>
                    % endif |
                    <a href="/admin/backup/download/${backup}">下载</a> |
                    % if permit.match(current_user.username,'/admin/backup/restore'):
                    <a href="javascript:restoredb('${backup}')">恢复</a>
                    % endif
                </td>
            </tr>
            % endfor
            </tbody>
        </table>
    </div>
            </div>
</div>
</section>

</%def>
